package com.lyshop.jinxiang.activitys;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.lyshop.jinxiang.R;
import com.lyshop.jinxiang.application.MyApplication;
import com.lyshop.jinxiang.base.BaseActivity;
import com.lyshop.jinxiang.bean.UserInfoBean;
import com.lyshop.jinxiang.net.ActionCallbackListener;
import com.lyshop.jinxiang.utils.DataUtils;
import com.lyshop.jinxiang.utils.ToastUtil;

public class EditNickNameActivity extends BaseActivity implements View.OnClickListener {

    private EditText et_nickname;
    private Button btn_submit;

    @Override
    protected int setContentView() {
        return R.layout.activity_edit_nickname;
    }

    @Override
    protected void findViews() {
        et_nickname = $(R.id.et_nickname);
        btn_submit  =  $(R.id.btn_submit);
        UserInfoBean bean = DataUtils.getUserInfo(mContext);
        et_nickname.setText(bean.nickname);
    }

    @Override
    protected void setListeners() {
        btn_submit.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if(view.getId() == R.id.btn_submit){
            if(et_nickname.getText().length() == 0 || et_nickname.getText().length() <6 || et_nickname.getText().length() >12){
                ToastUtil.showToast(mContext,"请输入6-12位的昵称");
                return;
            }
            UserInfoBean bean = DataUtils.getUserInfo(mContext);
            if(et_nickname.getText().equals(bean.nickname)){
                ToastUtil.showToast(mContext,"请修改后新昵称后保存");
                return;
            }
            if(et_nickname.getText().toString().contains(" ")){
                ToastUtil.showToast(mContext,"昵称中不能包含空格");
                return;
            }
        }
        mAppAction = MyApplication.getApplication().getAppAction();
        mAppAction.editUserNickName(et_nickname.getText().toString(), new ActionCallbackListener<Void>() {
            @Override
            public void onSuccess(Void data) {
                ToastUtil.showToast(mContext, "操作成功");
                finish();
            }

            @Override
            public void onFailure(String errorCode, String message) {
                ToastUtil.showToast(mContext,message);
            }
        });

    }
}
